/*
 * AtrapaTecla.java
 * Copyright (C) 04/01/2006, 23:46 Lhrod
 * Modified (C) 06/02/2010
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 */

package Sindarin.Gui;

import Robot.Robot;


/**
 *
 * @author fran
 */
public class AtrapaTecla implements Runnable {

    private MainPanel padre;
    private Thread thread = null;
    private int keyCode;
    private Robot robot;

    private boolean stopped = true;
    
    /** Creates a new instance of AtrapaTecla */
    public AtrapaTecla(MainPanel padre, Robot robot, int keyCode) {
        thread = new Thread (this , this.getClass().getCanonicalName());
        thread.setPriority(Thread.MAX_PRIORITY);
        this.robot = robot;
        this.padre = padre;
        this.keyCode = keyCode;
    }
    
    public void run() {
        while (!robot.isKeyPress((byte)keyCode) && !stopped)
            try {
                Thread.sleep(30);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        padre.stop();
        stopped = true;
    }
    
    public void start() {
        stopped = false;
        if (robot.isNativeSystem()) thread.start();
    }
    
    public void stop() {
        stopped = true;
    }
    
    public boolean isStop() {
        return stopped;
    }
}